package com.eurasia.assignment.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.transaction.annotation.Transactional;

import com.eurasia.assignment.domain.UserRole;
import com.eurasia.assignment.security.JwtTokenUtil;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
public class GetClassByIdTest {

    @Autowired
    private MockMvc mockMvc;
    
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    
    private String jwtToken;
    
    @BeforeEach
    public void setup() {
        // 创建一个模拟用户并生成JWT令牌
        com.eurasia.assignment.domain.User testUser = new com.eurasia.assignment.domain.User();
        testUser.setUsername("testuser");
        testUser.setRole(UserRole.TEACHER); // 使用TEACHER角色
        
        // 生成JWT令牌
        jwtToken = jwtTokenUtil.generateToken(testUser);
    }

    @Test
    @Sql("/sql/class.sql")
    public void should_get_class_by_id() throws Exception {
        // 构建请求并添加Authorization头
        MockHttpServletRequestBuilder request = get("/classes/1")
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", "Bearer " + jwtToken);
        
        // 执行请求并验证结果
        mockMvc.perform(request)
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value(1))
                .andExpect(jsonPath("$.className").value("Test Class 1"))
                .andExpect(jsonPath("$.grade").value("2023"))
                .andExpect(jsonPath("$.major").value("Computer Science"))
                .andExpect(jsonPath("$.tutor").value("John Doe"))
                .andExpect(jsonPath("$.studentCount").value(30))
                .andExpect(jsonPath("$.remark").value("Test class remark"));
    }
    
    @Test
    @Sql("/sql/class.sql")
    public void should_return_not_found_for_non_existent_class() throws Exception {
        // 构建请求并添加Authorization头
        MockHttpServletRequestBuilder request = get("/classes/999")
                .contentType(MediaType.APPLICATION_JSON)
                .header("Authorization", "Bearer " + jwtToken);
        
        // 执行请求并验证结果
        mockMvc.perform(request)
                .andExpect(status().isNotFound());
    }
    
    @Test
    public void shouldReturnUnauthorizedWithoutToken() throws Exception {
        // 执行没有认证令牌的请求
        mockMvc.perform(get("/classes/1")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isForbidden());
    }
} 